﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Assignment1.WeatherForcasts;
using Assignment1.EncryptionService;
using System.Xml;
using System.Net;
using System.IO;

namespace Assignment1
{
    public partial class WebBrowserForm : Form
    {
        //************************************************************************
        //*
        //*  Member Variables
        //*
        //************************************************************************

        private Assignment1.WeatherForcasts.WeatherForecasts weatherForcasts;
        private Assignment1.WeatherForcasts.WeatherForecastSoapClient weatherForcast;
        private WeatherData[] weather;
        private Bitmap pic;

        private System.Windows.Forms.GroupBox[] groupBoxes;
        private System.Windows.Forms.PictureBox[] pictureBoxes;
        private System.Windows.Forms.Label[] lowLabels;
        private System.Windows.Forms.Label[] highLabels;

        private Assignment1.EncryptionService.ServiceSoapClient encryption_client;

        //************************************************************************
        //*
        //*  Constructor
        //*
        //************************************************************************
        public WebBrowserForm()
        {
            InitializeComponent();

            groupBoxes = new GroupBox[5] { _firstGroup, _secondGroup, _thirdGroup, _forthGroup, _fifthGroup };
            pictureBoxes = new System.Windows.Forms.PictureBox[5] { _firstImage, _secondImage, _thirdImage, _forthImage, _fifthImage };
            lowLabels = new System.Windows.Forms.Label[5] { _lowTemp1, _lowTemp2, _lowTemp3, _lowTemp4, _lowTemp5 };
            highLabels = new System.Windows.Forms.Label[5] { _highTemp1, _highTemp2, _highTemp3, _highTemp4, _highTemp5 };

            weatherForcasts = new WeatherForecasts();
            weatherForcast = new WeatherForecastSoapClient("WeatherForecastSoap");

            for (int i = 0; i < 5; ++i)
            {
                pictureBoxes[i].SizeMode = PictureBoxSizeMode.StretchImage;
            }

            encryption_client = new ServiceSoapClient("ServiceSoap");
        }

        //************************************************************************
        //*
        //* Go button was clicked.  Open the requested url in the browser window
        //*
        //************************************************************************

        private void _goButton_Click(object sender, EventArgs e)
        {
            try
            {
                _browserWindow.Navigate(_url.Text);
            }
            catch (Exception ex)
            {
                String message =
                    "Exception caught while trying to open url: " +
                    _url.Text +
                    ".  Exception was: " +
                    ex.Message;

                Console.WriteLine(message);
            }
        }

        private void _forcastButton_Click(object sender, EventArgs e)
        {
            try
            {
                // attempt to get the weather forcast from the service
                Assignment1.WeatherForcasts.WeatherForecasts forcasts =
                    weatherForcast.GetWeatherByZipCode(_zipCode.Text);

                if (forcasts.Details != null)
                {
                    weather = forcasts.Details;
                    for (int i = 0; i < 5; ++i)
                    {
                        // get the date and load into the text of the group box
                        groupBoxes[i].Text = weather[i].Day;

                        // get the picture
                        Bitmap wImage = load_picture(weather[i].WeatherImage);
                        if (wImage != null)
                        {
                            pictureBoxes[i].Image = wImage;
                        }

                        // get the low and high temperatures and load in to the low/high labels
                        lowLabels[i].Text = weather[i].MinTemperatureF;
                        highLabels[i].Text = weather[i].MaxTemperatureF;

                    }
                }
                else
                {
                    //_messages.AppendText("No weather forcasts found for zip code: " + _zipCode.Text + "\r\n");
                }
            }
            catch (Exception ex)
            {
                String message =
                    "Exception caught while attempting to fetch weather forcast.  " +
                    "Exception: " + ex.Message + "\r\n";
                Console.WriteLine(message);

                //_messages.AppendText(message);
            }
            try
            {
                USZipService.USZipSoapClient zip_service = new USZipService.USZipSoapClient("USZipSoap");
                System.Xml.XmlNode node = zip_service.GetInfoByZIP(_zipCode.Text);
                System.Xml.XPath.XPathNavigator xnav = node.CreateNavigator();
                System.Xml.XPath.XPathNodeIterator iterator = xnav.Select("/NewDataSet/Table/City");
                String location = iterator.Current.Value;

                if (location.Length > 11)
                {
                    location = location.Substring(0, location.Length - 11);
                }
                else
                {
                    location = "Unable to get location information";
                }

                _zipCodeInfo.Text = location;

            }
            catch (Exception ex)
            {
                String message =
                    "Exception caught while attempting to fetch zip code location information.  " +
                    "Exception: " + ex.Message + "\r\n";
                Console.WriteLine(message);

                //_messages.AppendText(message);
            }
        }

        //************************************************************************
        //*
        //*  Load a picture from a URL
        //*
        //************************************************************************

        private Bitmap load_picture(string url)
        {
            System.Net.HttpWebRequest wreq = null;
            System.Net.HttpWebResponse wresp = null;
            Stream stream = null;
            Bitmap bitmap = null;

            try
            {
                wreq = (HttpWebRequest)WebRequest.Create(url);
                wreq.AllowWriteStreamBuffering = true;

                wresp = (HttpWebResponse)wreq.GetResponse();
                if ((stream = wresp.GetResponseStream()) != null)
                {
                    bitmap = new Bitmap(stream);
                }
            }
            catch (Exception ex)
            {
                String message =
                    "Exception caught while trying to load image. Exception: "
                    + ex.Message + "\r\n";

                Console.WriteLine(message);
                //_messages.AppendText(message);
            }
            finally
            {
                if (stream != null) stream.Close();
                if (wresp != null) wresp.Close();
            }
            return bitmap;
        }

        //************************************************************************
        //*
        //*  Encrypt button was pressed.
        //*
        //************************************************************************

        private void _encryptButton_Click(object sender, EventArgs e)
        {
            // get the string to encrypt
            String to_encrypt = _stringToEncrypt.Text;

            // encrypt the string
            to_encrypt = encryption_client.Encrypt(to_encrypt);

            // show the encrypted string in the to decrypt textbox
            _stringToDecrypt.Text = to_encrypt;

        }

        //************************************************************************
        //*
        //*  Decrypt button was pressed.
        //*
        //************************************************************************

        private void _decryptButton_Click(object sender, EventArgs e)
        {
            // get the string to encrypt
            String to_decrypt = _stringToDecrypt.Text;

            // encrypt the string
            to_decrypt = encryption_client.Decrypt(to_decrypt);

            // show the encrypted string in the to decrypt textbox
            _stringToEncrypt.Text = to_decrypt;
        }
    }
}
